<!DOCTYPE html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta name="google-site-verification" content="-YPbgjltFVFljoCm04Ktk0m38A8CkQxpifuKVZXtawA">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">






















<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/avatar.gif?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/avatar.gif?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/avatar.gif?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/avatar.gif?v=5.1.4" color="#222">





  <meta name="keywords" content="java,mybatis,">





  <link rel="alternate" href="/blog/atom.xml" title="lfagon" type="application/atom+xml">






<meta name="description" content="配置 mybatis 需要导入相应的 mybatis 的包和数据库驱动的包，需要注意版本问题 在项目 src 下配置mybatis-config.xml和database-connected.properties 配置database-connected.properties文件的目的是方便修改数据库连接配置  mybatis-config.xml 文件 默认顺序，不可颠倒，但是有些可以不设 12">
<meta name="keywords" content="java,mybatis">
<meta property="og:type" content="article">
<meta property="og:title" content="mybatis 使用">
<meta property="og:url" content="http://lfagon.gitee.io/blog/2018/12/18/mybatis使用/index.html">
<meta property="og:site_name" content="lfagon">
<meta property="og:description" content="配置 mybatis 需要导入相应的 mybatis 的包和数据库驱动的包，需要注意版本问题 在项目 src 下配置mybatis-config.xml和database-connected.properties 配置database-connected.properties文件的目的是方便修改数据库连接配置  mybatis-config.xml 文件 默认顺序，不可颠倒，但是有些可以不设 12">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2019-06-21T07:04:18.945Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="mybatis 使用">
<meta name="twitter:description" content="配置 mybatis 需要导入相应的 mybatis 的包和数据库驱动的包，需要注意版本问题 在项目 src 下配置mybatis-config.xml和database-connected.properties 配置database-connected.properties文件的目的是方便修改数据库连接配置  mybatis-config.xml 文件 默认顺序，不可颠倒，但是有些可以不设 12">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Mist',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":true,"onmobile":false},
    fancybox: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://lfagon.gitee.io/blog/2018/12/18/mybatis使用/">





  <title>mybatis 使用 | lfagon</title>
  








  <!-- Global site tag (gtag.js) - Google Analytics -->
  <script async src="https://www.googletagmanager.com/gtag/js?id=UA-139884627-1"></script>
  <script>
    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
    gtag('js', new Date());
    gtag('config', 'UA-139884627-1');
  </script>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">

    <div class="headband"></div>
    

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/" class="brand" rel="start">
        <!-- <span class="logo-line-before"><i></i></span> -->
        <!-- <span class="site-title">lfagon</span> -->
	     <img src="/blog/images/logo.png">
        <!-- <span class="logo-line-after"><i></i></span> -->
      </a>
    </div>
      
        <h1 class="site-subtitle" itemprop="description">个人博客</h1>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/blog/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-top">
          <a href="/blog/top/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-signal"></i> <br>
            
            热门
          </a>
        </li>
      
        
        <li class="menu-item menu-item-schedule">
          <a href="/blog/schedule/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-calendar"></i> <br>
            
            日程表
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tracks">
          <a href="/blog/tracks" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-comment"></i> <br>
            
            留言
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br>
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://lfagon.gitee.io/blog/blog/2018/12/18/mybatis使用/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="付祥/lfagon">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/blog/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="lfagon">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">mybatis 使用</h2>
        

        <div class="post-meta">
	    
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <font color="blue"><i class="fa fa-calendar-o"></i></font>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-12-18T11:02:03+08:00">
                2018-12-18
              </time>
            

            
              <span class="post-meta-divider">|</span>
            

            
              <span class="post-meta-item-icon">
                 <font color="#40E0D0"><i class="fa fa-calendar-check-o"></i></font>
              </span>
              
                <span class="post-meta-item-text">更新于&#58;</span>
              
              <time title="更新于" itemprop="dateModified" datetime="2019-06-21T15:04:18+08:00">
                2019-06-21
              </time>
            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <font color="red"><i class="fa fa-folder-o"></i></font>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <font color="black"><span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/用法/" itemprop="url" rel="index">
                    <span itemprop="name">用法</span>
                  </a>
                </span></font>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/blog/2018/12/18/mybatis使用/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count gitment-comments-count" data-xid="/blog/2018/12/18/mybatis使用/" itemprop="commentsCount"></span>
                </a>
              </span>
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  6.2k
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  25
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>配置 mybatis 需要导入相应的 mybatis 的包和数据库驱动的包，需要<strong>注意版本问题</strong></p>
<p>在项目 src 下配置<code>mybatis-config.xml</code>和<code>database-connected.properties</code></p>
<p>配置<code>database-connected.properties</code>文件的目的是方便修改数据库连接配置</p>
<hr>
<h1>mybatis-config.xml 文件</h1>
<p><strong>默认顺序，不可颠倒，但是有些可以不设</strong></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version="1.0" encoding="UTF-8" ?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">!</span> <span class="attr">DOCTYPE</span> <span class="attr">configuration</span></span></span><br><span class="line"><span class="tag">        <span class="attr">PUBLIC</span> "<span class="attr">-</span>//<span class="attr">mybatis.org</span>//<span class="attr">DTD</span> <span class="attr">Config</span> <span class="attr">3.0</span>//<span class="attr">EN</span>"</span></span><br><span class="line"><span class="tag">        "<span class="attr">http:</span>//<span class="attr">mybatis.org</span>/<span class="attr">dtd</span>/<span class="attr">mybatis-3-config.dtd</span>"&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">configuration</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">properties</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">settings</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">typeAliases</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">typeHandlers</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">objectFactory</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">plugins</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">environments</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">databaseIdProvider</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">mappers</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">configuration</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h1>基本配置，可以直接复制使用</h1>
<ul>
<li>这里配置可以直接复制使用，如果需要不常用配置，将它复制到相应注释位置</li>
<li>需要修改一些参数，根据实际情况修改</li>
</ul>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version="1.0" encoding="UTF-8" ?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">!</span> <span class="attr">DOCTYPE</span> <span class="attr">configuration</span></span></span><br><span class="line"><span class="tag">        <span class="attr">PUBLIC</span> "<span class="attr">-</span>//<span class="attr">mybatis.org</span>//<span class="attr">DTD</span> <span class="attr">Config</span> <span class="attr">3.0</span>//<span class="attr">EN</span>"</span></span><br><span class="line"><span class="tag">        "<span class="attr">http:</span>//<span class="attr">mybatis.org</span>/<span class="attr">dtd</span>/<span class="attr">mybatis-3-config.dtd</span>"&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">configuration</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--下面的外层标签配置顺序不可以颠倒，有些设置可以不写--&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!--properties--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">properties</span> <span class="attr">resource</span>=<span class="string">"database-connected.properties"</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"driver"</span> <span class="attr">value</span>=<span class="string">"driver"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"url"</span> <span class="attr">value</span>=<span class="string">"url"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"username"</span> <span class="attr">value</span>=<span class="string">"username"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"password"</span> <span class="attr">value</span>=<span class="string">"password"</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">properties</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!--settings设置--&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!--typeAliases类型取别名--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">typeAliases</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">package</span> <span class="attr">name</span>=<span class="string">"com.ksd.common.data.pojo"</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">typeAliases</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!--typeHandlers类型处理器--&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!--objectFactory对象工厂--&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!--environments配置环境--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">environments</span> <span class="attr">default</span>=<span class="string">"development"</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!--environment环境变量--&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">environment</span> <span class="attr">id</span>=<span class="string">"development"</span>&gt;</span></span><br><span class="line">            <span class="comment">&lt;!--事务管理器--&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">transactionManager</span> <span class="attr">type</span>=<span class="string">"JDBC"</span>/&gt;</span></span><br><span class="line">            <span class="comment">&lt;!--数据源--&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">dataSource</span> <span class="attr">type</span>=<span class="string">"POOLED"</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"driver"</span> <span class="attr">value</span>=<span class="string">"$&#123;driver&#125;"</span>/&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"url"</span> <span class="attr">value</span>=<span class="string">"$&#123;url&#125;"</span>/&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"username"</span> <span class="attr">value</span>=<span class="string">"$&#123;username&#125;"</span>/&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"password"</span> <span class="attr">value</span>=<span class="string">"$&#123;password&#125;"</span>/&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">dataSource</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">environment</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">environments</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!--databaseIdProvider数据库厂商标识--&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!--配置映射文件--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">mappers</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">mapper</span> <span class="attr">class</span>=<span class="string">"com.ksd.common.data.mapper. SchoolMapper"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">mapper</span> <span class="attr">class</span>=<span class="string">"com.ksd.common.data.mapper. MajorMapper"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">mapper</span> <span class="attr">class</span>=<span class="string">"com.ksd.common.data.mapper. OpusMapper"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">mapper</span> <span class="attr">class</span>=<span class="string">"com.ksd.common.data.mapper. UserMapper"</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">mappers</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">configuration</span>&gt;</span></span><br></pre></td></tr></table></figure>
<hr>
<h1>properties 详解</h1>
<p>配置一些运行参数，properties 相当于给变量取别名，在后面要用到使用别名即可。</p>
<p>这三种的优先级是程序传递》properties 文件》子元素传递</p>
<p>一般都需要对用户名和密码做加密处理，这样的话第二种和第三种方法配合使用时最好的</p>
<ol>
<li>子元素传递</li>
</ol>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">properties</span> <span class="attr">resource</span>=<span class="string">"database-connected.properties"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"driver"</span> <span class="attr">value</span>=<span class="string">"com.mysql.jdbc. Driver"</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"url"</span> <span class="attr">value</span>=<span class="string">"jdbc:mysql://(ip地址）:3306/(数据库名字）?(参数）&amp;(参数）"</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"username"</span> <span class="attr">value</span>=<span class="string">"username"</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"password"</span> <span class="attr">value</span>=<span class="string">"password"</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">properties</span>&gt;</span></span><br></pre></td></tr></table></figure>
<ol start="2">
<li>使用 properties 文件，properties 请看下面 db-connected.properties 文件配置</li>
</ol>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--</span></span><br><span class="line"><span class="comment">如果加了resource="db-connected.properties"是配置外部</span></span><br><span class="line"><span class="comment">properties属性文件，通过外部元素来传递</span></span><br><span class="line"><span class="comment">name是xml用的名字，引用时需要用$&#123;&#125;括起来，value是properties里的key</span></span><br><span class="line"><span class="comment">--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">properties</span> <span class="attr">resource</span>=<span class="string">"db-connected.properties"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"driver"</span> <span class="attr">value</span>=<span class="string">"driver"</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"url"</span> <span class="attr">value</span>=<span class="string">"url"</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"username"</span> <span class="attr">value</span>=<span class="string">"username"</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"password"</span> <span class="attr">value</span>=<span class="string">"password"</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">properties</span>&gt;</span></span><br></pre></td></tr></table></figure>
<ol start="3">
<li>使用程序传递方式传递参数</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Properties p=readEncryptionProperties();</span><br><span class="line">InputStream in = Resources.getResourceAsStream(mybatisConfig);</span><br><span class="line"><span class="comment">//用读取过来的properties配置，覆盖掉从xml文件里的配置</span></span><br><span class="line">sqlSessionFactory=<span class="keyword">new</span> SqlSessionFactoryBuilder().build(in, p);</span><br></pre></td></tr></table></figure>
<p>读取加密的用户名和密码，并解密，返回一个 Properties 文件</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">private</span> Properties <span class="title">readEncryptionProperties</span><span class="params">()</span></span>&#123;</span><br><span class="line">    logger.debug(<span class="string">"encrypte username and password"</span>);</span><br><span class="line">    Properties p=<span class="keyword">new</span> Properties();</span><br><span class="line">    InputStream in=<span class="keyword">null</span>;</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">        String mybatisConfigPath=Class.class</span><br><span class="line">            .getResource(<span class="string">"/db-connected.properties"</span>).toURI().getPath();</span><br><span class="line">        in= <span class="keyword">new</span> FileInputStream(mybatisConfigPath);</span><br><span class="line">        p.load(in);</span><br><span class="line">        String username= (String) p.get(D_U);</span><br><span class="line">        String password= (String) p.get(D_P);</span><br><span class="line">        username=Encryption.decrypt_Base64(username);</span><br><span class="line">        password=Encryption.decrypt_Base64(password);</span><br><span class="line">        logger.debug(username);</span><br><span class="line">        logger.debug(password);</span><br><span class="line">        p.put(D_U, username);</span><br><span class="line">        p.put(D_P, password);</span><br><span class="line">    &#125; <span class="keyword">catch</span> (FileNotFoundException e) &#123;</span><br><span class="line">        logger.info(<span class="string">"not find "</span>+mybatisConfig);</span><br><span class="line">        e.printStackTrace();</span><br><span class="line">    &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">        logger.info(<span class="string">"not read file"</span>+mybatisConfigPath);</span><br><span class="line">        e.printStackTrace();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> p;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>加密解密</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Encryption</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> Logger logger= LogManager.getLogger(Encryption.class);</span><br><span class="line">    <span class="comment">/***</span></span><br><span class="line"><span class="comment">     * Base64加密</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> str需要加密的参数</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     * <span class="doctag">@throws</span> Exception</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">encrypt_Base64</span><span class="params">(String str)</span></span>&#123;</span><br><span class="line">        String result = <span class="keyword">null</span>;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            result = Base64.getEncoder().encodeToString(str.getBytes(<span class="string">"UTF-8"</span>));</span><br><span class="line">        &#125; <span class="keyword">catch</span> (UnsupportedEncodingException e) &#123;</span><br><span class="line">            logger.info(<span class="string">"not support other encoding, only utf-8"</span>);</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> result;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/***</span></span><br><span class="line"><span class="comment">     * Base64解密</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> str需要解密的参数</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     * <span class="doctag">@throws</span> Exception</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">decrypt_Base64</span><span class="params">(String str)</span></span>&#123;</span><br><span class="line">        <span class="keyword">byte</span>[] asBytes = Base64.getDecoder().decode(str);</span><br><span class="line">        String result = <span class="keyword">null</span>;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            result = <span class="keyword">new</span> String(asBytes, <span class="string">"UTF-8"</span>);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (UnsupportedEncodingException e) &#123;</span><br><span class="line">            logger.info(<span class="string">"not support other encoding, only utf-8"</span>);</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> result;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1>environments 详解</h1>
<ol>
<li>POOLED</li>
</ol>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">environments</span> <span class="attr">default</span>=<span class="string">"development"</span>&gt;</span></span><br><span class="line">       <span class="comment">&lt;!--environment环境变量--&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">environment</span> <span class="attr">id</span>=<span class="string">"development"</span>&gt;</span></span><br><span class="line">           <span class="comment">&lt;!--事务管理器--&gt;</span></span><br><span class="line">           <span class="tag">&lt;<span class="name">transactionManager</span> <span class="attr">type</span>=<span class="string">"JDBC"</span>/&gt;</span></span><br><span class="line">           <span class="comment">&lt;!--数据源--&gt;</span></span><br><span class="line">           <span class="tag">&lt;<span class="name">dataSource</span> <span class="attr">type</span>=<span class="string">"POOLED"</span>&gt;</span></span><br><span class="line">               <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"driver"</span> <span class="attr">value</span>=<span class="string">"$&#123;driver&#125;"</span>/&gt;</span></span><br><span class="line">               <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"url"</span> <span class="attr">value</span>=<span class="string">"$&#123;url&#125;"</span>/&gt;</span></span><br><span class="line">               <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"username"</span> <span class="attr">value</span>=<span class="string">"$&#123;username&#125;"</span>/&gt;</span></span><br><span class="line">               <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"password"</span> <span class="attr">value</span>=<span class="string">"$&#123;password&#125;"</span>/&gt;</span></span><br><span class="line">               <span class="comment">&lt;!--正在连接数，默认值10--&gt;</span></span><br><span class="line">               <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"poolMaximumActiveConnections"</span> <span class="attr">value</span>=<span class="string">"10"</span>/&gt;</span></span><br><span class="line">               <span class="comment">&lt;!--任意时间可能存在的空闲连接数--&gt;</span></span><br><span class="line">               <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"poolMaximumIdleConnections"</span> <span class="attr">value</span>=<span class="string">""</span>/&gt;</span></span><br><span class="line">               <span class="comment">&lt;!--强制返回前检测时间20000毫秒--&gt;</span></span><br><span class="line">               <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"poolMaximumCheckoutTime"</span> <span class="attr">value</span>=<span class="string">"20000"</span>/&gt;</span></span><br><span class="line">               <span class="comment">&lt;!--连接花费相等时间，它会给连接池打印状态，并重新连接默认20000毫秒--&gt;</span></span><br><span class="line">               <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"poolTimeToWait"</span> <span class="attr">value</span>=<span class="string">"20000"</span>/&gt;</span></span><br><span class="line">               <span class="comment">&lt;!--检测连接是否处于正常工作秩序，连接失败有错误信息，默认是NO PING QUERY SET--&gt;</span></span><br><span class="line">               <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"poolPingQuery"</span> <span class="attr">value</span>=<span class="string">"NO PING QUERY SET"</span>/&gt;</span></span><br><span class="line">               <span class="comment">&lt;!--是否启用侦测查询，默认false--&gt;</span></span><br><span class="line">               <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"poolPingEnable"</span> <span class="attr">value</span>=<span class="string">"false"</span>/&gt;</span></span><br><span class="line">               <span class="comment">&lt;!--配置poolPingQuery使用频率，来避免不要的侦测，默认0,每一时刻都在侦测--&gt;</span></span><br><span class="line">               <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"poolPingConnectionsNotUsedFor"</span> <span class="attr">value</span>=<span class="string">"0"</span>/&gt;</span></span><br><span class="line">           <span class="tag">&lt;/<span class="name">dataSource</span>&gt;</span></span><br><span class="line">       <span class="tag">&lt;/<span class="name">environment</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;/<span class="name">environments</span>&gt;</span></span><br></pre></td></tr></table></figure>
<ol start="2">
<li>UNPOOLED</li>
</ol>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">environments</span> <span class="attr">default</span>=<span class="string">"development"</span>&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">environment</span> <span class="attr">id</span>=<span class="string">"development"</span>&gt;</span></span><br><span class="line">           <span class="tag">&lt;<span class="name">transactionManager</span> <span class="attr">type</span>=<span class="string">"JDBC"</span>/&gt;</span></span><br><span class="line">           <span class="tag">&lt;<span class="name">dataSource</span> <span class="attr">type</span>=<span class="string">"UNPOOLED"</span>&gt;</span></span><br><span class="line">               <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"driver"</span> <span class="attr">value</span>=<span class="string">"$&#123;driver&#125;"</span>/&gt;</span></span><br><span class="line">               <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"url"</span> <span class="attr">value</span>=<span class="string">"$&#123;url&#125;"</span>/&gt;</span></span><br><span class="line">               <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"username"</span> <span class="attr">value</span>=<span class="string">"$&#123;username&#125;"</span>/&gt;</span></span><br><span class="line">               <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"password"</span> <span class="attr">value</span>=<span class="string">"$&#123;password&#125;"</span>/&gt;</span></span><br><span class="line">               <span class="comment">&lt;!--连接事务隔离等级--&gt;</span></span><br><span class="line">               <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"defaultTransactionIsolationLevel"</span> <span class="attr">value</span>=<span class="string">""</span>/&gt;</span></span><br><span class="line">           <span class="tag">&lt;/<span class="name">dataSource</span>&gt;</span></span><br><span class="line">       <span class="tag">&lt;/<span class="name">environment</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;/<span class="name">environments</span>&gt;</span></span><br></pre></td></tr></table></figure>
<ol start="3">
<li>JNDI</li>
</ol>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">environments</span> <span class="attr">default</span>=<span class="string">"development"</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">environment</span> <span class="attr">id</span>=<span class="string">"development"</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">transactionManager</span> <span class="attr">type</span>=<span class="string">"JDBC"</span>/&gt;</span></span><br><span class="line">            <span class="comment">&lt;!--这里type写上外部的数据源的全限定名--&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">dataSource</span> <span class="attr">type</span>=<span class="string">""</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"driver"</span> <span class="attr">value</span>=<span class="string">"$&#123;driver&#125;"</span>/&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"url"</span> <span class="attr">value</span>=<span class="string">"$&#123;url&#125;"</span>/&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"username"</span> <span class="attr">value</span>=<span class="string">"$&#123;username&#125;"</span>/&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"password"</span> <span class="attr">value</span>=<span class="string">"$&#123;password&#125;"</span>/&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">dataSource</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">environment</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">environments</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h1>typeAliases 详解</h1>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">typeAliases</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--将一个包下全取别名--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">package</span> <span class="attr">name</span>=<span class="string">"com.ksd.common.data.pojo"</span>/&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--将一个类取别名，type全限定名，alias别名--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">typeAlias</span> <span class="attr">type</span>=<span class="string">"com.ksd.common.data.pojo. OpusFile"</span> <span class="attr">alias</span>=<span class="string">"OpusFile"</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">typeAliases</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h1>mappers 映射器详解</h1>
<p>常用的也就是前三个引入，根据实际情况使用</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">mappers</span>&gt;</span></span><br><span class="line">       <span class="comment">&lt;!--文件路径引入--&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">mapper</span> <span class="attr">resource</span>=<span class="string">"com/ksd/common/data/mapper/SchoolMapper.xml"</span>/&gt;</span></span><br><span class="line">       <span class="comment">&lt;!--包名引入--&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">package</span> <span class="attr">name</span>=<span class="string">"com.ksd.common.data.mapper"</span>/&gt;</span></span><br><span class="line">       <span class="comment">&lt;!--用类注册引入--&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">mapper</span> <span class="attr">class</span>=<span class="string">"com.ksd.common.data.mapper. SchoolMapper"</span>/&gt;</span></span><br><span class="line">       <span class="comment">&lt;!--用url引入--&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">mapper</span> <span class="attr">url</span>=<span class="string">"file:///var/mapperscom/ksd/common/data/mapper/SchoolMapper.xml"</span>/&gt;</span></span><br><span class="line">   <span class="tag">&lt;/<span class="name">mappers</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h1>不常用配置</h1>
<p>如果需要配置，将夏明需要的复制到上面相应注释位置</p>
<h2>setting 详解</h2>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--settings设置--&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="name">settings</span>&gt;</span></span><br><span class="line">       <span class="comment">&lt;!--所有映射器缓存的全局开关</span></span><br><span class="line"><span class="comment">       默认true--&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"cacheEnabled"</span> <span class="attr">value</span>=<span class="string">""</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">       <span class="comment">&lt;!--延迟加载的全局开关，当开启时对象都会延迟加载</span></span><br><span class="line"><span class="comment">       特定关联可以通过fetchType来覆盖该项配置</span></span><br><span class="line"><span class="comment">       默认true--&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"lazyLoadingEnabled"</span> <span class="attr">value</span>=<span class="string">""</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">       <span class="comment">&lt;!--层级加载，开启时，对延迟加载属性对象完整加载</span></span><br><span class="line"><span class="comment">       反之则按需加载</span></span><br><span class="line"><span class="comment">       3.41版本默认false--&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"aggressiveLazyLoading"</span> <span class="attr">value</span>=<span class="string">""</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">       <span class="comment">&lt;!--是否单一语句返回多结果集（需要驱动兼容）</span></span><br><span class="line"><span class="comment">       默认true--&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"multipleResultSetsEnabled"</span> <span class="attr">value</span>=<span class="string">""</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">       <span class="comment">&lt;!--使用列标签替代列名</span></span><br><span class="line"><span class="comment">       默认true--&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"useColumnLabel"</span> <span class="attr">value</span>=<span class="string">""</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">       <span class="comment">&lt;!--允许JDBC自动生成主键</span></span><br><span class="line"><span class="comment">       默认false--&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"useGeneratedKey"</span> <span class="attr">value</span>=<span class="string">""</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">       <span class="comment">&lt;!--指定mybatis如何自动映射列到字段或属性</span></span><br><span class="line"><span class="comment">       NONE取消自动映射</span></span><br><span class="line"><span class="comment">       默认PARTIAL自动映射</span></span><br><span class="line"><span class="comment">       FULL自动映射复杂的结果集（无论是否嵌套）--&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"autoMapperBehavior"</span> <span class="attr">value</span>=<span class="string">""</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">       <span class="comment">&lt;!--指定自动映射当中未知列时的行为</span></span><br><span class="line"><span class="comment">       当日志级别达到warn级别或以下显示相关日志</span></span><br><span class="line"><span class="comment">       处理失败则抛出SqlSessionException异常</span></span><br><span class="line"><span class="comment">       默认NONE不处理，</span></span><br><span class="line"><span class="comment">       WARNING</span></span><br><span class="line"><span class="comment">       FAILING--&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"autoMapperUnknownColumnBehavior"</span> <span class="attr">value</span>=<span class="string">""</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">       <span class="comment">&lt;!--配置默认的执行器</span></span><br><span class="line"><span class="comment">       默认SIMPLE普通执行器</span></span><br><span class="line"><span class="comment">       REUSE会重用预处理语句（prepared statements)</span></span><br><span class="line"><span class="comment">       BATCH将重用语句并批量更新--&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"defaultExecutorType"</span> <span class="attr">value</span>=<span class="string">""</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">       <span class="comment">&lt;!--设置超时时间，它取决于数据库响应秒数</span></span><br><span class="line"><span class="comment">       默认空不设置</span></span><br><span class="line"><span class="comment">       可以设置任何正整数--&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"defaultStatementTimeout"</span> <span class="attr">value</span>=<span class="string">"60"</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">       <span class="comment">&lt;!--设置数据库驱动返回条数限制</span></span><br><span class="line"><span class="comment">       默认空不设置</span></span><br><span class="line"><span class="comment">       可以设置任何正整数--&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"defaultFetchSize"</span> <span class="attr">value</span>=<span class="string">""</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">       <span class="comment">&lt;!--允许在嵌套语句使用分页（RowBounds)</span></span><br><span class="line"><span class="comment">       允许设置为false</span></span><br><span class="line"><span class="comment">       默认false--&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"safeRowBoundsEnabled"</span> <span class="attr">value</span>=<span class="string">"false"</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">       <span class="comment">&lt;!--允许在嵌套语句使用分页（ResultHandler)</span></span><br><span class="line"><span class="comment">       允许设置为false</span></span><br><span class="line"><span class="comment">       默认true--&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"safeResultHandlerEnabled"</span> <span class="attr">value</span>=<span class="string">"true"</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">       <span class="comment">&lt;!--是否开启自动驼峰命名规则</span></span><br><span class="line"><span class="comment">       即从经典数据库列名a_column</span></span><br><span class="line"><span class="comment">       到java的aColumn的映射</span></span><br><span class="line"><span class="comment">       默认false--&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"mapUnderscoreToCamelCase"</span> <span class="attr">value</span>=<span class="string">"false"</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">       <span class="comment">&lt;!--利用本地缓存机制，防止循环引用和加速重复嵌套查询</span></span><br><span class="line"><span class="comment">       默认SESSION会缓存一个会话中执行的所有查询</span></span><br><span class="line"><span class="comment">       STATEMENT本地会话只用在语句执行上，相同的SQLSession</span></span><br><span class="line"><span class="comment">       不同的调用将不会共享数据--&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"localCacheScope"</span> <span class="attr">value</span>=<span class="string">"SESSION"</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">       <span class="comment">&lt;!--当结果集中值为null,是否调用映射对象的set方法</span></span><br><span class="line"><span class="comment">       （map的put方法），基本类型不能设置为null(int, boolean等）</span></span><br><span class="line"><span class="comment">       默认false--&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"callSettersOnNulls"</span> <span class="attr">value</span>=<span class="string">""</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">       <span class="comment">&lt;!--指定MyBatis增加到日志名称的前缀</span></span><br><span class="line"><span class="comment">       任何字符串--&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"logPrefix"</span> <span class="attr">value</span>=<span class="string">""</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">       <span class="comment">&lt;!--创建具有延迟加载能力的对象用到的代理工具</span></span><br><span class="line"><span class="comment">       CGLIB和JAVASSIST</span></span><br><span class="line"><span class="comment">       mybatis的3.3版本默认JAVASSIST--&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"proxyFactory"</span> <span class="attr">value</span>=<span class="string">""</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">       <span class="comment">&lt;!--指定vfs的实现类</span></span><br><span class="line"><span class="comment">       填写vfs类的全限定名</span></span><br><span class="line"><span class="comment">       多个vfs类可以用逗号分隔--&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"vfsImpl"</span> <span class="attr">value</span>=<span class="string">""</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">       <span class="comment">&lt;!--允许用方法参数中声明的实际名称引用参数</span></span><br><span class="line"><span class="comment">       使用此功能必须编译为java 8以上</span></span><br><span class="line"><span class="comment">       mybatis的版本3.4.1以上才可以使用</span></span><br><span class="line"><span class="comment">       默认true--&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"useActualParamName"</span> <span class="attr">value</span>=<span class="string">""</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">       <span class="comment">&lt;!--指定mybatis所用日志具体实现，未指定时将自动找</span></span><br><span class="line"><span class="comment">       SLF4J, LOG4J, LOG4J2, JDK_LOGGING</span></span><br><span class="line"><span class="comment">       COMMONS_LOGGING, STDOUT_LOGGING</span></span><br><span class="line"><span class="comment">       NO_LOGGING</span></span><br><span class="line"><span class="comment">       默认为空--&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"logImpl"</span> <span class="attr">value</span>=<span class="string">""</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">       <span class="comment">&lt;!--当没有为参数指定JDBC类型时，指定列的JDBC类型</span></span><br><span class="line"><span class="comment">       默认OTHER</span></span><br><span class="line"><span class="comment">       NULL</span></span><br><span class="line"><span class="comment">       VARCHAR--&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"jdbcTypeForNull"</span> <span class="attr">value</span>=<span class="string">""</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">       <span class="comment">&lt;!--指定动态SQL生成的默认语言，指定驱动全限定名</span></span><br><span class="line"><span class="comment">       默认org.apache.ibatis.scripting.xmltags. XMLLanguageDriver--&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"defaultScriptingLanguage"</span> <span class="attr">value</span>=<span class="string">""</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">       <span class="comment">&lt;!--指定哪个对象的方法触发一次延迟加载</span></span><br><span class="line"><span class="comment">       默认equals, clone, hashCode, toString--&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"lazyLoadTriggerMethods"</span> <span class="attr">value</span>=<span class="string">""</span>/&gt;</span></span><br><span class="line">   <span class="tag">&lt;/<span class="name">settings</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h2>typeHandlers 详解</h2>
<p>这个就是将数据库里的类型数据转换成 java 里面有的数据类型，mybatis 在一般场景都会自动转换，特殊场景就需要自定义，这时候就要实现接口<code>org.apache.ibatis.type.TypeHandler</code></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--typeHandlers类型转换器--&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="name">typeHandlers</span>&gt;</span></span><br><span class="line">       <span class="comment">&lt;!--</span></span><br><span class="line"><span class="comment">	handler处理器的全限定名实现了接口</span></span><br><span class="line"><span class="comment">	org.apache.ibatis.type. TypeHandler</span></span><br><span class="line"><span class="comment">       javaType在java中的类型</span></span><br><span class="line"><span class="comment">       jdbcType在数据库中的类型</span></span><br><span class="line"><span class="comment">       --&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">typeHandler</span> <span class="attr">handler</span>=<span class="string">""</span> <span class="attr">javaType</span>=<span class="string">""</span> <span class="attr">jdbcType</span>=<span class="string">""</span>/&gt;</span></span><br><span class="line">   <span class="tag">&lt;/<span class="name">typeHandlers</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>使用时在 result 加入<code>typeHandler</code>参数，例如</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">resultMap</span> <span class="attr">id</span>=<span class="string">""</span> <span class="attr">type</span>=<span class="string">""</span>&gt;</span></span><br><span class="line">       <span class="comment">&lt;!--在typeHandler添加类型转换器类的全限定名--&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">""</span> <span class="attr">column</span>=<span class="string">""</span> <span class="attr">typeHandler</span>=<span class="string">""</span>/&gt;</span></span><br><span class="line">   <span class="tag">&lt;/<span class="name">resultMap</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h2>objectFactory 详解</h2>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--objectFactory对象工厂</span></span><br><span class="line"><span class="comment">   如果要自己实现实现这个接口</span></span><br><span class="line"><span class="comment">   org.apache.ibatis.reflection.factory. ObjectFactory</span></span><br><span class="line"><span class="comment">   type是实现对象工厂的全限定名，默认是</span></span><br><span class="line"><span class="comment">   org.apache.ibatis.reflection.factory. DefaultObjectFactory--&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="name">objectFactory</span> <span class="attr">type</span>=<span class="string">""</span>&gt;</span></span><br><span class="line"></span><br><span class="line">   <span class="tag">&lt;/<span class="name">objectFactory</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h2>plugins 详解</h2>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2>databaseIdProvider 详解</h2>
<p>当使用多个数据 SQL 时需要配置该属性，该属性被配置时，会优先取用与该配置一致的 SQL, 如果没有则取用</p>
<p><code>databaseId</code>的 SQL, 最后还是取不到则抛出异常，说明无法匹配对应的 SQL</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">   <span class="comment">&lt;!--databaseIdProvider数据库厂商标识</span></span><br><span class="line"><span class="comment">默认使用自带的标识，DB_VENDOR即是</span></span><br><span class="line"><span class="comment">org.apache.ibatis.mapping. VendorDatabaseIdProvider</span></span><br><span class="line"><span class="comment">自定义请实现接口</span></span><br><span class="line"><span class="comment">org.apache.ibatis.mapping. DatabaseIdProvider</span></span><br><span class="line"><span class="comment">type是实现该接口的全限定名</span></span><br><span class="line"><span class="comment">name和value绑定在一起，即等同</span></span><br><span class="line"><span class="comment">--&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="name">databaseIdProvider</span> <span class="attr">type</span>=<span class="string">"DB_VENDOR"</span>&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"MySql"</span> <span class="attr">value</span>=<span class="string">"mysql"</span>/&gt;</span></span><br><span class="line">   <span class="tag">&lt;/<span class="name">databaseIdProvider</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>需要使用时在相应的语句中加入<code>databaseId</code>参数，例如</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">"selectSchool"</span> <span class="attr">padatabaseId</span>=<span class="string">"mysql"</span>&gt;</span></span><br><span class="line">    select * from school where school_id=#&#123;school_id&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h1>db-connected.properties 文件</h1>
<h2>基本配置</h2>
<ul>
<li>这里的配置可以复制使用</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">password和username最好使用base64，rsa等加密，使用前解密</span><br><span class="line">password = Q29tbW9uQGMxMjM0NTY\ =</span><br><span class="line">    username = Y29tbW9u</span><br><span class="line"></span><br><span class="line"> mysql5用的驱动url是com.mysql.jdbc.Driver# mysql6以后用的是com.mysql.cj.jdbc.Driver</span><br><span class="line">driver = com.mysql.jdbc.Driver</span><br><span class="line"></span><br><span class="line"> 需要加参数直接加 &amp; (参数）url = jdbc: mysql: //(ip地址）:3306/(数据库名字）?(参数）&amp;(参数）</span><br></pre></td></tr></table></figure>
<p><a href="https://blog.csdn.net/lfagon/article/details/84568317" target="_blank" rel="noopener">mysql里的url详解</a></p>
<hr>
<h1>mybatis 类映射器配置文件</h1>
<p>配置完成 mybatis 之后需要配置相应的 xxxxxxMapper.java 和 xxxxxxMapper.xml, xxxxxxMapper.java 里面就是接口而具体操作都是在 xxxxxxMapper.xml 里配置</p>
<p>有几个地方需要注意</p>
<ul>
<li>Mapper.java 和 Mapper.xml 经编译后需要在同一个目录下</li>
<li>Mapper.java 和 Mapper.xml 名字要一致</li>
<li>Mapper.java 和 Mapper.xml 写好后需要在 mybatis 主配置文件里配置<mapper>参数，切记</mapper></li>
<li>Mapper.xml 文件里的命名空间就是 Mapper.java 的全限定名</li>
<li>一般来说一个 pojo 类对应一个 Mapper.java 和一个 Mapper.xml</li>
</ul>
<p>像一般的操作我们不必每个都去写SQL语句，这里有个叫mybatis-generator-core自动生成mybatis的类映射器配置，相关使用请见<a href="http://mbg.cndocs.ml/index.html" target="_blank" rel="noopener">官方文档</a></p>
<h2>Mapper.xml 的几个元素</h2>
<table>
<thead>
<tr>
<th style="text-align:center">元素名称</th>
<th style="text-align:center">描述</th>
<th style="text-align:center">备注</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">select</td>
<td style="text-align:center">查询语句</td>
<td style="text-align:center">可以自定义参数，返回结果集</td>
</tr>
<tr>
<td style="text-align:center">insert</td>
<td style="text-align:center">插入语句</td>
<td style="text-align:center">执行后返回整数代表插入行数</td>
</tr>
<tr>
<td style="text-align:center">update</td>
<td style="text-align:center">更新语句</td>
<td style="text-align:center">执行后返回整数代表更新行数</td>
</tr>
<tr>
<td style="text-align:center">delete</td>
<td style="text-align:center">删除语句</td>
<td style="text-align:center">执行后返回整数代表删除行数</td>
</tr>
<tr>
<td style="text-align:center">sql</td>
<td style="text-align:center">允许定义一部分 sql, 然后在各个地方引用</td>
<td style="text-align:center">例如一张表的列名</td>
</tr>
<tr>
<td style="text-align:center">resultMap</td>
<td style="text-align:center">用来从数据库结果集中加载对象，这是最复杂，最强大的元素</td>
<td style="text-align:center">提供映射规则</td>
</tr>
<tr>
<td style="text-align:center">cache</td>
<td style="text-align:center">给定命名空间的缓存配置</td>
<td style="text-align:center"></td>
</tr>
<tr>
<td style="text-align:center">cache-ref</td>
<td style="text-align:center">其他命名空间缓存配置的引用</td>
<td style="text-align:center"></td>
</tr>
</tbody>
</table>
<h2>select 配置参数</h2>
<p>这里的参数有些地方需要注意，容易出错</p>
<ul>
<li>
<p>resultMap 需要自己定义，下面会详细讲述</p>
</li>
<li>
<p>resultType 和 resultMap 只能使用其中一个</p>
</li>
<li>
<p>resultMap 是用来定义返回复杂的集合嵌套问题及 pojo 类的属性名和数据列名不符合规范的问题</p>
</li>
<li>
<p>需要使用 resultMap 的情况</p>
<ul>
<li>一个对象包含集合类型</li>
<li>pojo 类属性名和数据列名不符合规范，即从 a_column 和 aColumn 的类似映射</li>
</ul>
</li>
</ul>
<p>select 属性表</p>
<table>
<thead>
<tr>
<th>属性</th>
<th style="text-align:center">描述</th>
<th style="text-align:center">备注</th>
</tr>
</thead>
<tbody>
<tr>
<td>id</td>
<td style="text-align:center">此命名空间中可用于引用此语句的唯一标识符，即 Mapper.java 文件操作数据库的方法名</td>
<td style="text-align:center">命名空间和 id 结合起来，要唯一，否则抛出异常</td>
</tr>
<tr>
<td>parameterType</td>
<td style="text-align:center">将传递给此语句的参数的完全限定类名或别名。此属性是可选的，因为 MyBatic 可以计算 TypeHandler 以使用传递给语句的实际参数</td>
<td style="text-align:center">可以是单个参数或多个参数</td>
</tr>
<tr>
<td>resultType</td>
<td style="text-align:center">将从此语句返回的预期类型的完全限定类名或别名。注意，在集合的情况下，这应该是集合包含的类型，而不是集合本身的类型</td>
<td style="text-align:center">不能和 resultMap 一起使用</td>
</tr>
<tr>
<td>resultMap</td>
<td style="text-align:center">对外部结果映射的命名引用。resultMap 是 mybatis 最强大的特征，只要对它们有很好的理解，就可以解决许多难以解决的映射问题</td>
<td style="text-align:center">不能和 resultType 一起使用</td>
</tr>
<tr>
<td>databaseId</td>
<td style="text-align:center">如果有一个配置好的 databaseIdProvider, mybatis 加载所有没有数据库 ID 属性或与当前语句匹配的数据库 ID 的语句。如果是大小写，则如果找到带有或不带数据库 Id 的相同语句，则后者将被丢弃</td>
<td style="text-align:center"></td>
</tr>
<tr>
<td>flushCache</td>
<td style="text-align:center">将此设置为 true 将导致每当调用此语句时本地和第 2 级缓存被刷新</td>
<td style="text-align:center">默认为 false</td>
</tr>
<tr>
<td>useCache</td>
<td style="text-align:center">将此设置为 true 将导致将此语句的结果缓存在第二级缓存中</td>
<td style="text-align:center">默认为 true</td>
</tr>
<tr>
<td>timeout</td>
<td style="text-align:center">这将设置驱动程序等待数据库从请求返回的秒数，然后抛出异常</td>
<td style="text-align:center">数据厂商提供默认秒数</td>
</tr>
<tr>
<td>fetchSize</td>
<td style="text-align:center">这是一个驱动程序提示，它将试图使驱动程序返回与此设置相同大小的成批行编号结果</td>
<td style="text-align:center">数据厂商提供默认条数</td>
</tr>
<tr>
<td>statementType</td>
<td style="text-align:center"><code>STATEMENT</code>,<code>PREPARED</code>,<code>CALLABLE</code>使用对应的类对象分别为<code>Statement</code>,<code>PreparedStatement</code>，<code>CallableStatement</code></td>
<td style="text-align:center">默认<code>PREPARED</code></td>
</tr>
<tr>
<td>resultSetType</td>
<td style="text-align:center"><code>FORWARD_ONLY</code>,<code>SCROLL_SENSITIVE</code>,<code>SCROLL_INSENSITIVE</code></td>
<td style="text-align:center">数据厂商提供默认值</td>
</tr>
<tr>
<td>resultOrdered</td>
<td style="text-align:center">这仅适用于嵌套的结果 SELECT 语句： 如果为真，则假定嵌套的结果包含在一起或分组在一起，这样当返回新的主结果行时，将不再出现对前一个结果行的引用。这使得嵌套的结果能够被填充得更友好的内存</td>
<td style="text-align:center">默认为 false</td>
</tr>
<tr>
<td>resultSets</td>
<td style="text-align:center">这仅适用于多个结果集。它列出了将由语句返回的结果集，并给出了每个结果集的名称。名字用逗号隔开</td>
<td style="text-align:center">很少使用</td>
</tr>
</tbody>
</table>
<h2>insert, update, delete 配置参数</h2>
<p>insert, update, delete 属性表</p>
<table>
<thead>
<tr>
<th style="text-align:center">属性</th>
<th style="text-align:center">描述</th>
<th style="text-align:center">备注</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">id</td>
<td style="text-align:center">此命名空间中可用于引用此语句的唯一标识符，即 Mapper.java 文件操作数据库的方法名</td>
<td style="text-align:center">命名空间和 id 结合起来，要唯一，否则抛出异常</td>
</tr>
<tr>
<td style="text-align:center">parameterType</td>
<td style="text-align:center">将传递给此语句的参数的完全限定类名或别名。此属性是可选的，因为 mybatis 可以计算 TypeHandler 以使用传递给语句的实际参数</td>
<td style="text-align:center">可以是单个参数或多个参数</td>
</tr>
<tr>
<td style="text-align:center">databaseId</td>
<td style="text-align:center">如果有一个配置好的数据库 IdProvider, mybatis 将加载所有没有数据库 ID 属性或与当前语句匹配的数据库 ID 的语句。如果是大小写，则如果找到带有或不带数据库 Id 的相同语句，则后者将被丢弃。</td>
<td style="text-align:center"></td>
</tr>
<tr>
<td style="text-align:center">flushCache</td>
<td style="text-align:center">将此设置为 true 将导致每当调用此语句时本地和第 2 级缓存被刷新</td>
<td style="text-align:center">默认为 true</td>
</tr>
<tr>
<td style="text-align:center">timeout</td>
<td style="text-align:center">这将设置驱动程序在引发异常之前等待数据库从请求返回的最大秒数</td>
<td style="text-align:center">单位为秒</td>
</tr>
<tr>
<td style="text-align:center">statementType</td>
<td style="text-align:center"><code>STATEMENT</code>,<code>PREPARED</code>,<code>CALLABLE</code>使用对应的类对象分别为<code>Statement</code>,<code>PreparedStatement</code>，<code>CallableStatement</code></td>
<td style="text-align:center">默认<code>PREPARED</code></td>
</tr>
<tr>
<td style="text-align:center">useGeneratedKeys</td>
<td style="text-align:center">（仅插入和更新），这告诉 mybatis 使用 JDBCgetGeneratedKeys 方法检索数据库内部生成的密钥（例如，数据库中的自动增量字段），返回主键</td>
<td style="text-align:center">默认为 false</td>
</tr>
<tr>
<td style="text-align:center">keyProperty</td>
<td style="text-align:center">（仅插入和更新）标识一个属性，其中 MyBtis 将设置 getGeneratedKeys 或 INSERT 语句的 selectKey 子元素返回的键值。默认： 未设置。如果需要多个生成的列，则可以是逗号分隔的属性名称列表</td>
<td style="text-align:center">默认不设置，不能与 keyColumn 连用</td>
</tr>
<tr>
<td style="text-align:center">keyColumn</td>
<td style="text-align:center">（仅插入和更新）使用生成的键设置表中列的名称。只有在某些数据库（如 PostgreSQL) 中，键列不是表中的第一列时才需要这样做。如果需要多个生成的列，则可以为逗号分隔的列表。</td>
<td style="text-align:center">默认不设置，不能与 keyProperty 连用</td>
</tr>
</tbody>
</table>
<h2>resultMap 配置参数</h2>
<h3>resultMap 属性</h3>
<table>
<thead>
<tr>
<th style="text-align:center">属性</th>
<th style="text-align:center">描述</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">id</td>
<td style="text-align:center">resultMap 标签的标识</td>
</tr>
<tr>
<td style="text-align:center">type</td>
<td style="text-align:center">返回值的全限定类名，或类型别名</td>
</tr>
<tr>
<td style="text-align:center">autoMapping</td>
<td style="text-align:center">设置是否启动自动映射功能，自动映射功能就是自动查找与字段名小写同名的属性名，并调用 setter 方法。而设置为 false 后，则需要在<code>resultMap</code>内明确注明映射关系才会调用对应的 setter 方法，默认 false</td>
</tr>
</tbody>
</table>
<h3>resultMap 子元素</h3>
<p>常用属性配置细节</p>
<table>
<thead>
<tr>
<th>属性</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>property</td>
<td>需要映射到 JavaBean 的属性名称。</td>
</tr>
<tr>
<td>column</td>
<td>数据表的列名或者标签别名。</td>
</tr>
<tr>
<td>javaType</td>
<td>一个完整的类名，或者是一个类型别名。如果你匹配的是一个 JavaBean, 那 MyBatis 通常会自行检测到。然后，如果你是要映射到一个 HashMap, 那你需要指定 javaType 要达到的目的。</td>
</tr>
<tr>
<td>jdbcType</td>
<td>数据表支持的类型列表。这个属性只在 insert, update 或 delete 的时候针对允许空的列有用。JDBC 需要这项，但 MyBatis 不需要。如果你是直接针对 JDBC 编码，且有允许空的列，而你要指定这项。</td>
</tr>
</tbody>
</table>
<p>一般属性配置细节</p>
<table>
<thead>
<tr>
<th>属性</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>ofType</td>
<td>集合里对象的全限定名</td>
</tr>
<tr>
<td>select</td>
<td>另一个映射语句的 ID, 该语句将加载此属性映射所需的复杂类型。从列属性中指定的列检索的值将作为参数传递给目标 SELECT 语句</td>
</tr>
<tr>
<td>typeHandler</td>
<td>使用这个属性可以覆写类型处理器。这项值可以是一个完整的类名，也可以是一个类型别名。</td>
</tr>
<tr>
<td>fetchType</td>
<td>可选的，eager 和 lazy, 如果存在，它将取代为此映射启用的全局配置参数 lazyLoadingEnable</td>
</tr>
<tr>
<td>resultMap</td>
<td>这是 ResultMap 的 ID, 它可以将该关联的嵌套结果映射到适当对象中。这是对另一个 SELECT 语句的调用的替代方法。它允许您将多个表连接到一个 ResultSet 中。这样的 ResultSet 将包含重复的， 重复的数据组，这些数据组需要被分解并正确地映射到嵌套的对象中，MyBaties 允许您将“链式”结果映射放在一起，以处理嵌套的结果</td>
</tr>
<tr>
<td>columnPrefix</td>
<td>在连接多个表时，必须使用列别名以避免在 ResultSet 中重复列名。指定 ColumnPrefix 允许您将这些列映射到外部结果映射</td>
</tr>
<tr>
<td>notNullColumn</td>
<td>默认情况下，只有在映射到子属性的列中至少有一个为非空时，才会创建子对象。使用此属性，您可以通过指定哪些列必须具有一个值来更改这种行为，因此，只有当这些列中的任何一列为非空时，MyBtis 才会创建一个子对象。可以使用逗号作为分隔符指定多个列名。默认值：unset.</td>
</tr>
<tr>
<td>resultSet</td>
<td>标识将从其中加载此复杂类型的结果集的名称。</td>
</tr>
<tr>
<td>foreignColumn</td>
<td>标识包含外键的列的名称，这些值将与父类型的列 attibute 中指定的列值相匹配。</td>
</tr>
<tr>
<td>autoMapping</td>
<td>设置是否启动自动映射功能，自动映射功能就是自动查找与字段名小写同名的属性名，并调用 setter 方法。而设置为 false 后，则需要在<code>resultMap</code>内明确注明映射关系才会调用对应的 setter 方法，默认 false</td>
</tr>
</tbody>
</table>
<ul>
<li>
<p><strong>id</strong>用于设置主键字段与领域模型属性的映射关系</p>
</li>
<li>
<p><strong>result</strong> 用于设置普通字段与领域模型属性的映射关系</p>
</li>
<li>
<p><strong>constructor</strong> 用来将结果反射给一个实例化好的类的构造器</p>
<ul>
<li><strong>idArg</strong>ID 参数。 标记结果作为 ID 可以帮助提高整体效能。</li>
<li><strong>arg</strong> – 注入到构造方法的一个普通参数。</li>
</ul>
</li>
</ul>
<p>我们使用 id，result 时候，需要定义 java 实体类的属性映射到数据库表的字段上，这个时候是使用 JavaBean 实现的。当然我们也可以使用实体类的构造方法来实现值的映射，这个时候是通过构造方法参数的书写的顺序来进行赋值的。使用 construcotr 功能有限（例如使用 collection 级联查询）</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">resultMap</span> <span class="attr">type</span>=<span class="string">"StudentEntity"</span> <span class="attr">id</span>=<span class="string">"studentResultMap"</span> &gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">constructor</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">idArg</span> <span class="attr">javaType</span>=<span class="string">"String"</span> <span class="attr">column</span>=<span class="string">"STUDENT_ID"</span>/&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">arg</span> <span class="attr">javaType</span>=<span class="string">"String"</span> <span class="attr">column</span>=<span class="string">"STUDENT_NAME"</span>/&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">arg</span> <span class="attr">javaType</span>=<span class="string">"String"</span> <span class="attr">column</span>=<span class="string">"STUDENT_SEX"</span>/&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">arg</span> <span class="attr">javaType</span>=<span class="string">"Date"</span> <span class="attr">column</span>=<span class="string">"STUDENT_BIRTHDAY"</span>/&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">constructor</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">resultMap</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>java 文件中写法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="title">StudentEntity</span><span class="params">(String studentID, String studentName, String studentSex,</span></span></span><br><span class="line"><span class="function"><span class="params">                     Date studentBirthday)</span></span>&#123;</span><br><span class="line">     <span class="keyword">this</span>.studentID = studentID;</span><br><span class="line">     <span class="keyword">this</span>.studentName = studentName;</span><br><span class="line">     <span class="keyword">this</span>.studentSex = studentSex;</span><br><span class="line">     <span class="keyword">this</span>.studentBirthday = studentBirthday;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>
<p><strong>discriminator</strong>鉴别器</p>
<ul>
<li>case– 基本一些值的结果映射的 case 情形</li>
</ul>
</li>
</ul>
<p>有时一个单独的数据库查询也许返回很多不同，数据类型的结果集。鉴别器元素就是被设计来处理这个情况的，还有包括类的继承层次结构。鉴别器非常容易理解，因为它的表现很像 Java 语言中的 switch 语句。</p>
<p>定义鉴别器指定了 column 和 javaType 属性。列是 MyBatis 查找比较值的地方。JavaType 是需要被用来保证等价测试的合适类型（尽管字符串在很多情形下都会有用）</p>
<p>下面这个例子为，当 classId 为 20000001 时，才映射 classId 属性。</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">resultMap</span> <span class="attr">type</span>=<span class="string">"liming.student.manager.data.model. StudentEntity"</span> <span class="attr">id</span>=<span class="string">"resultMap_studentEntity_discriminator"</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="name">discriminator</span> <span class="attr">column</span>=<span class="string">"CLASS_ID"</span> <span class="attr">javaType</span>=<span class="string">"String"</span> <span class="attr">jdbcType</span>=<span class="string">"VARCHAR"</span>&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">case</span> <span class="attr">value</span>=<span class="string">"20000001"</span> <span class="attr">resultType</span>=<span class="string">"liming.student.manager.data.model. StudentEntity"</span> &gt;</span></span><br><span class="line">             <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">"classId"</span> <span class="attr">column</span>=<span class="string">"CLASS_ID"</span> <span class="attr">javaType</span>=<span class="string">"String"</span> <span class="attr">jdbcType</span>=<span class="string">"VARCHAR"</span>/&gt;</span></span><br><span class="line">         <span class="tag">&lt;/<span class="name">case</span>&gt;</span></span><br><span class="line">     <span class="tag">&lt;/<span class="name">discriminator</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;/<span class="name">resultMap</span>&gt;</span></span><br></pre></td></tr></table></figure>
<ul>
<li><strong>association</strong> 复杂类型的结合</li>
</ul>
<p>联合元素用来处理“一对一”的关系</p>
<ul>
<li><strong>collection</strong>复杂类型集合</li>
</ul>
<p>集合元素，用来处理一对多的关系</p>

      
    </div>

    
      





    


    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>如果这篇文章对您很有帮助，不妨</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/blog/images/wechatpay.png" alt="付祥/lfagon 微信支付">
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/blog/images/alipay.jpg" alt="付祥/lfagon 支付宝">
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    
    
    	<div>
	   
             <div>
    
        <div style="margin-top: 30px;text-align:center;color: #ccc;font-size:14px;">-------------　　　　本文结束　<i class="fa fa-heart"></i>　感谢您的阅读　　　　-------------</div>
    
</div>
           
        </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/blog/tags/java/" rel="tag"><i class="fa fa-tag"></i> java</a>
          
            <a href="/blog/tags/mybatis/" rel="tag"><i class="fa fa-tag"></i> mybatis</a>
          
        </div>
      

      
      
        <div class="post-widgets">
        
          <div class="wp_rating">
            <div style="color: rgba(0, 0, 0, 0.75); font-size:13px; letter-spacing:3px">(&gt;看完记得五星好评哦亲&lt;)</div>
            <div id="wpac-rating"></div>
          </div>
        

        

        
          
          <div id="needsharebutton-postbottom">
            <span class="btn">
              <i class="fa fa-share-alt" aria-hidden="true"></i>
            </span>
          </div>
        
        </div>
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2018/12/16/支付宝网页接入流程/" rel="next" title="支付宝网页接入流程">
                <i class="fa fa-chevron-left"></i> 支付宝网页接入流程
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2019/02/10/使用ssh连接linux主机/" rel="prev" title="使用 ssh 连接 linux 主机">
                使用 ssh 连接 linux 主机 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
        
  <div class="bdsharebuttonbox">
    <a href="#" class="bds_tsina" data-cmd="tsina" title="分享到新浪微博"></a>
    <a href="#" class="bds_douban" data-cmd="douban" title="分享到豆瓣网"></a>
    <a href="#" class="bds_sqq" data-cmd="sqq" title="分享到QQ好友"></a>
    <a href="#" class="bds_qzone" data-cmd="qzone" title="分享到QQ空间"></a>
    <a href="#" class="bds_weixin" data-cmd="weixin" title="分享到微信"></a>
    <a href="#" class="bds_tieba" data-cmd="tieba" title="分享到百度贴吧"></a>
    <a href="#" class="bds_twi" data-cmd="twi" title="分享到Twitter"></a>
    <a href="#" class="bds_fbook" data-cmd="fbook" title="分享到Facebook"></a>
    <a href="#" class="bds_more" data-cmd="more"></a>
    <a class="bds_count" data-cmd="count"></a>
  </div>
  <script>
    window._bd_share_config = {
      "common": {
        "bdText": "",
        "bdMini": "2",
        "bdMiniList": false,
        "bdPic": ""
      },
      "share": {
        "bdSize": "24",
        "bdStyle": "0"
      },
      "image": {
        "viewList": ["tsina", "douban", "sqq", "qzone", "weixin", "twi", "fbook"],
        "viewText": "分享到：",
        "viewSize": "7"
      }
    }
  </script>

<script>
  with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='/static/api/js/share.js?cdnversion='+~(-new Date()/36e5)];
</script>

      
  </div>


          </div>
          

  
    <div class="comments" id="comments">
      
        <div id="gitment-container"></div>
      
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/blog/images/avatar.jpg" alt="付祥/lfagon">
            
              <p class="site-author-name" itemprop="name">付祥/lfagon</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">77</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/blog/categories/index.html">
                  <span class="site-state-item-count">6</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/blog/tags/index.html">
                  <span class="site-state-item-count">28</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/blog/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/lfagon" target="_blank" title="Github">
                      
                        <i class="fa fa-fw fa-github"></i></a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://gitee.com/lfagon" target="_blank" title="Gitee">
                      
                        <i class="fa fa-fw fa-git"></i></a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mailto:slash7675@gmail.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i></a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://plus.google.com/" target="_blank" title="Google">
                      
                        <i class="fa fa-fw fa-google"></i></a>
                  </span>
                
            </div>
          

          
          
            <div class="cc-license motion-element" itemprop="license">
              <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" class="cc-opacity" target="_blank">
                <img src="/blog/images/cc-by-nc-sa.svg" alt="Creative Commons">
              </a>
            </div>
          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-block">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-globe"></i>
                圈子里住着一群可爱的程序猿&nbsp;<i class="fa fa-fw fa-globe fa-flip-horizontal"></i>
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="https://reuixiy.github.io/" title="一个Hexo博客玩到极致的大佬" target="_blank">一个Hexo博客玩到极致的大佬</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://mmmmasterc.github.io/" title="又菜又爱秀的哥们" target="_blank">又菜又爱秀的哥们</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.codesheep.cn/" title="乐于分享的华科Java程序羊(B站up主)" target="_blank">乐于分享的华科Java程序羊(B站up主)</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://me.idealli.com/" title="前端大神自称兰州小红鸡" target="_blank">前端大神自称兰州小红鸡</a>
                  </li>
                
              </ul>
            </div>
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#undefined"><span class="nav-number">1.</span> <span class="nav-text">mybatis-config.xml 文件</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#undefined"><span class="nav-number">2.</span> <span class="nav-text">基本配置，可以直接复制使用</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#undefined"><span class="nav-number">3.</span> <span class="nav-text">properties 详解</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#undefined"><span class="nav-number">4.</span> <span class="nav-text">environments 详解</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#undefined"><span class="nav-number">5.</span> <span class="nav-text">typeAliases 详解</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#undefined"><span class="nav-number">6.</span> <span class="nav-text">mappers 映射器详解</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#undefined"><span class="nav-number">7.</span> <span class="nav-text">不常用配置</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#undefined"><span class="nav-number">7.1.</span> <span class="nav-text">setting 详解</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#undefined"><span class="nav-number">7.2.</span> <span class="nav-text">typeHandlers 详解</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#undefined"><span class="nav-number">7.3.</span> <span class="nav-text">objectFactory 详解</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#undefined"><span class="nav-number">7.4.</span> <span class="nav-text">plugins 详解</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#undefined"><span class="nav-number">7.5.</span> <span class="nav-text">databaseIdProvider 详解</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#undefined"><span class="nav-number">8.</span> <span class="nav-text">db-connected.properties 文件</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#undefined"><span class="nav-number">8.1.</span> <span class="nav-text">基本配置</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#undefined"><span class="nav-number">9.</span> <span class="nav-text">mybatis 类映射器配置文件</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#undefined"><span class="nav-number">9.1.</span> <span class="nav-text">Mapper.xml 的几个元素</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#undefined"><span class="nav-number">9.2.</span> <span class="nav-text">select 配置参数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#undefined"><span class="nav-number">9.3.</span> <span class="nav-text">insert, update, delete 配置参数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#undefined"><span class="nav-number">9.4.</span> <span class="nav-text">resultMap 配置参数</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#undefined"><span class="nav-number">9.4.1.</span> <span class="nav-text">resultMap 属性</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#undefined"><span class="nav-number">9.4.2.</span> <span class="nav-text">resultMap 子元素</span></a></li></ol></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      
    </div>
  </aside>


        
      </div>
    </div></main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <font color="red"><i class="fa fa-heartbeat" aria-hidden="true"></i></font>
  </span>
  <span class="author" itemprop="copyrightHolder">copyright</span>
  &nbsp;<a href="https://www.oschina.net/"><font size="2px">中国开源社区</font></a>

  
</div>




<div style="width:300px;margin:0 auto; padding:0 0;">
   <a href style="display:inline-block;text-decoration:none;height:20px;line-height:20px;"><img src="/blog/images/icons/record.png" style="float:left;height:90%;width:auto;"><p style="float:left;height:20px;line-height:20px;margin: 0px 0px 0px 5px; color:#939393;letter-spacing:0px"><font size="2px">公网安备 xxxxxxxxxxx号</font></p></a>
</div>





        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv">
      <i class="fa fa-user">&nbsp;访客量：</i>
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
      
    </span>
  

  
    <span class="site-pv">
      <i class="fa fa-eye">&nbsp;访问量：</i>
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
      
    </span>
  
</div>




        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
          <span id="scrollpercent"><span>0</span>%</span>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>




















  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  


  

  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/image.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/linkcard.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/clicksocialvalue.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/clicklove.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  



	





  




  





<!-- LOCAL: You can save these files to your site and update links -->
    
        
        <link rel="stylesheet" href="https://aimingoo.github.io/gitmint/style/default.css">
        <script src="https://aimingoo.github.io/gitmint/dist/gitmint.browser.js"></script>
    
<!-- END LOCAL -->

    

    
      <script type="text/javascript">
      function renderGitment(){
        var gitment = new Gitmint({
            id: window.location.pathname, 
            owner: 'lfagon',
            repo: 'BlogComment',
            
            lang: "" || navigator.language || navigator.systemLanguage || navigator.userLanguage,
            
            oauth: {
            
            
                client_secret: 'de7d4aee11c1d7e4fd0ab75222817a4875fb378c',
            
                client_id: '969cf8ab6443ea4bd042'
            }});
        gitment.render('gitment-container');
      }

      
      renderGitment();
      
      </script>
    





  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/blog/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  
<script>
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
    }
    else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>


  
  
  
  <link rel="stylesheet" href="/blog/lib/needsharebutton/needsharebutton.css">

  
  
  <script src="/blog/lib/needsharebutton/needsharebutton.js"></script>

  <script>
    
      pbOptions = {};
      
          pbOptions.iconStyle = "box";
      
          pbOptions.boxForm = "horizontal";
      
          pbOptions.position = "bottomCenter";
      
          pbOptions.networks = "Weibo,Wechat,Douban,QQZone,Twitter,Facebook";
      
      new needShareButton('#needsharebutton-postbottom', pbOptions);
    
    
  </script>

  
  <script type="text/javascript">
  wpac_init = window.wpac_init || [];
  wpac_init.push({widget: 'Rating', id: 18607,
    el: 'wpac-rating',
    color: 'f79533'
  });
  (function() {
    if ('WIDGETPACK_LOADED' in window) return;
    WIDGETPACK_LOADED = true;
    var mc = document.createElement('script');
    mc.type = 'text/javascript';
    mc.async = true;
    mc.src = '//embed.widgetpack.com/widget.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(mc, s.nextSibling);
  })();
  </script>


  

  
<script>
  $('.highlight').each(function(i, e) {
    var $wrap = $('<div>').addClass('highlight-wrap');
    $(e).after($wrap);
    $wrap.append($('<button>').addClass('copy-btn').append('复制').on('click', function(e) {
      var code = $(this).parent().find('.code').find('.line').map(function(i, e) {
        return $(e).text();
      }).toArray().join('\n');
      var ta = document.createElement('textarea');
      var yPosition = window.pageYOffset || document.documentElement.scrollTop;
      ta.style.top = yPosition + 'px'; // Prevent page scroll
      ta.style.position = 'absolute';
      ta.style.opacity = '0';
      ta.readOnly = true;
      ta.value = code;
      document.body.appendChild(ta);
      ta.select();
      ta.setSelectionRange(0, code.length);
      ta.readOnly = false;
      var result = document.execCommand('copy');
      
        if (result) $(this).text('复制成功');
        else $(this).text('复制失败');
      
      ta.blur(); // For iOS
      $(this).blur();
    })).on('mouseleave', function(e) {
      var $b = $(this).find('.copy-btn');
      setTimeout(function() {
        $b.text('复制');
      }, 300);
    }).append(e);
  })
</script>


  

  

</body>
</html>
